<meta name="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
var shibie = 1;
$(function(){
	$("#productTab .nav-tabs li a").click(function(e){
		e.preventDefault();//阻止a链接的跳转行为 
        $(this).tab("show");//显示当前选中的链接及关联的content
        
        var tabIndex = $(this).attr("data");
        $("#productFormGroup form[name="+tabIndex+"Form]").show();
        $("#productFormGroup form:not([name="+tabIndex+"Form])").hide();
        
        //发出查询请求
        ajaxSearchList.apply(this,[tabIndex, 1, 10]);
	});
	
	//执行消费记录搜索
	$("#btn_consume_search").click(function(){
		ajaxSearchList("consumeList",1,10);
	});
	
	//执行产品记录搜索
	$("#btn_product_search").click(function(){
		ajaxSearchList("productList",1,10);
	});
	
	$("#btn_order_search").click(function(){
		ajaxSearchList("orderList",1,10);
	});
	
	if(index=="consumeList") {
		$("#sltMemberId").select2('val', memberId);
		$("#sltClassId").select2('val', classId);
		$("a[data='consumeList']").click();
	} else if(index=="productList") {
		$("a[data='productList']").click();
	} else {
		ajaxSearchList("orderList",1,10);
	}
});
var index = "${index}";
var memberId = "${memberId}";
var classId = "${classId}";

function ajaxSearchList(searchType, pageNo, pageSize){
	
	if(searchType == "orderList") {
		var orderProName = $("#txtProductName").val();
		var orderProType = $("#selProductOrderTypes").val();
		var buyStartDate = $("#buyStartDate").val();
		if(buyStartDate){
			buyStartDate +=" 00:00";
		}
		var buyEndDate = $("#buyEndDate").val();
		if(buyEndDate){
			buyEndDate+=" 23:59";
		}
		var buyerId = $("#selBuyers").val();
		var sellerId = $("#selSellers").val();
		var orderType = $("#orderType").val();
		var display = $("#display").val();
		var intPageNo = pageNo || 1;
		var intPageSize = pageSize || 10;
		
		var url = "yueyogaAdmin/ajaxGetOrderList.jhtml";
		$.post(
			url, 
			{
			"orderProName":orderProName,
			"orderProType":orderProType,
			"buyStartDate":buyStartDate,
			"buyEndDate":buyEndDate,
			"buyerId":buyerId,
			"sellerId":sellerId,
			"orderType":orderType,
			"pageNo":intPageNo,
			"pageSize":intPageSize
		}, function(json){
			var result = eval("("+json+")");
			var success = result.success;
			if(success){
				var totalPages = result.totalPages;
				var totalItems = result.totalItems;
				var pageNo = result.pageNo;
				var pageSize = result.pageSize;
				var prevPageNo = result.prevPageNo;
				var nextPageNo = result.nextPageNo;
				var items = result.items;//消费记录数组
				shibie=2;
				if(shibie==2){
					$("#btn_export").removeClass();
					$("#btn_export").addClass("btn btn-default panel-radius-all");
					$("#btn_add").hide();
					$("#btn_edit").hide();
					$("#btn_delete").hide();
				}
				//填充表表格中
				$("#orderList table tr:not(:eq(0))").remove();
				$.each(items, function(index,item){
					var tfje = item.money;
					if(item.buyType=="退款"){
						tfje = -tfje;
					}
					$("#orderList table").append("<tr><td>"+item.id+"</td><td><a href='yueyogaAdmin/toMemberProduct.jhtml?memberId=" + item.buyerId + "'>"+item.buyerName+"</a></td><td>" + item.products + "</td><td>" + item.buyType + "</td><td>"+tfje+"</td><td>"+item.productTypes+"</td><td>"+item.officerName+"</td><td>"+item.buyTime+"</td></tr>");
				});
				
				$(".orderPageBar ul.pagination").initPageBar({
					pageNo:pageNo,
					pageSize:pageSize,
					prevPageNo:prevPageNo,
					nextPageNo:nextPageNo,
					totalPages:totalPages,
					totalResults:totalItems
				}, function(jumpTo, pageSize){
					ajaxSearchList(searchType,jumpTo,pageSize);
				});
			} else {
				error(json.msg || "查询失败");
			}
		});
	} else if(searchType == "consumeList") {
		var classId = $("#sltClassId").val();
		var productId = $("#sltProductId").val();
		var memberId = $("#sltMemberId").val();
		var areaId = $("#sltAreaId").val();
		var consumeTimeFrom = $("#consumeTimeFrom").val();
		if(consumeTimeFrom){
			consumeTimeFrom+=" 00:00"
		}
		var consumeTimeTo = $("#consumeTimeTo").val();
		if(consumeTimeTo){
			consumeTimeTo +=" 23:59"
		}
		var scheduleTimeFrom = $("#scheduleTimeFrom").val();
		var scheduleTimeTo = $("#scheduleTimeTo").val();
		
		//累计未出席
		var totalAbsent = $("#total_absent").val();
		
		var intPageNo = pageNo || 1;
		var intPageSize = pageSize || 10;
		
		var url = "yueyogaAdmin/ajaxGetConsumeList.jhtml";
		$.post(url,{
			classId:classId,
			productId:productId,
			memberId:memberId,
			areaId:areaId,
			consumeTimeFrom:consumeTimeFrom,
			consumeTimeTo:consumeTimeTo,
			scheduleTimeFrom:scheduleTimeFrom,
			scheduleTimeTo:scheduleTimeTo,
			totalAbsent:totalAbsent,
			pageNo:intPageNo,
			pageSize:intPageSize
		}, function(json){
			var result = eval("("+json+")");
			var success = result.success;
			
			if(success){
				var totalPages = result.totalPages;
				var totalItems = result.totalItems;
				var pageNo = result.pageNo;
				var pageSize = result.pageSize;
				var prevPageNo = result.prevPageNo;
				var nextPageNo = result.nextPageNo;
				var items = result.items;//消费记录数组
				shibie=3;
				if(shibie==3){
					$("#btn_export").removeClass();
					$("#btn_export").addClass("btn btn-default panel-radius-all");
					$("#btn_add").hide();
					$("#btn_edit").hide();
					$("#btn_delete").hide();
				}
				//填充表表格中
				$("#consumeList table tr:not(:eq(0))").remove();
				$.each(items, function(index,item){
					$("#consumeList table").append("<tr><td>"+item.memberId+"</td><td>"+item.memberName+"</td><td>"+item.className+"</td><td>"+item.week+" "+item.scheduleStartTime+"~"+item.scheduleEndTime+"</td><td>"+item.scheduleTeacherName+"</td><td>"+item.scheduleAssistantName+"</td><td>"+item.schedulePlaceName+"</td><td>"+item.productName+"</td><td>"+item.consumeTime+"</td></tr>");
				});
				
				$(".consumePageBar ul.pagination").initPageBar({
					pageNo:pageNo,
					pageSize:pageSize,
					prevPageNo:prevPageNo,
					nextPageNo:nextPageNo,
					totalPages:totalPages,
					totalResults:totalItems
				}, function(jumpTo, pageSize){
					ajaxSearchList(searchType,jumpTo,pageSize);
				});
			}else {
				error(json.msg || "查询失败");
			}
		});
	}else if(searchType == "productList"){
		
		var productName = $("#productName").val();
		var productType = $("#productType").val();
		var startdate1 = $("#startdate1").val();
		var startdate2 = $("#startdate2").val();
		var enddate1 = $("#enddate1").val();
		var enddate2 = $("#enddate2").val();
		var intPageNo = pageNo || 1;
		var intPageSize = pageSize || 10;
		
		var url = "yueyogaAdmin/ajaxGetProductList.jhtml";
		$.post(url, {
			productName:productName,
			productType:productType,
			productId:productId,
			startdate1:startdate1,
			startdate2:startdate2,
			enddate1:enddate1,
			enddate2:enddate2,
			pageNo:intPageNo,
			pageSize:intPageSize
		}, function(json){
			
			var result = eval("("+json+")");
			var success = result.success;
			if(success){
				var totalPages = result.totalPages;
				var totalItems = result.totalItems;
				var pageNo = result.pageNo;
				var pageSize = result.pageSize;
				var prevPageNo = result.prevPageNo;
				var nextPageNo = result.nextPageNo;
				var items = result.items;//消费记录数组
				shibie=1;
				if(shibie==1){
					   $("#btn_export").removeClass();
					   $("#btn_export").addClass("btn btn-default panel-radius-right");
					   $("#btn_add").show();
					   $("#btn_edit").show();
					   $("#btn_delete").show();
					}
				//填充表表格中
				$("#productList table tr:not(:eq(0))").remove();
				$.each(items, function(index,item){
					var pro = item;
					//此逻辑应当在数据库关联表实现
					var proType;
					if(pro.type == 101){
						proType = "按次数";
					} else if(pro.type == 102) {
						proType = "按时间";
					} else if(pro.type == 107) {
						proType = "按金额";
					} else if(pro.type == 103) {
						proType = "年卡";
					} else if(pro.type == 104) {
						proType = "季卡";
					} else if(pro.type == 105) {
						proType = "月卡";
					}  else if(pro.type == 106) {
						proType = "半月卡";
					} else if(pro.type == 108) {
						proType = "实时扣费";
					}
					if(pro.limitOfWeek == undefined || pro.limitOfWeek == null || pro.limitOfWeek == ""){
						pro.limitOfWeek = "无限制";
					}else {
						pro.limitOfWeek += "次";
					}
					
					$("#productList table").append("<tr><td><input type='checkbox' id='chk_id' name='chk_name' value='"+pro.productId+"'></td><td>"+pro.productId+"</td><td><a href='yueyogaAdmin/toProductDetails.jhtml?productId="+pro.productId+"'>"+pro.name+"</a></td><td>"+proType+"</td><td>"+pro.price+"</td><td>"+pro.counts+"</td><td>"+pro.limitOfWeek+"</td><td>"+pro.strBeginTime+"</td><td>"+pro.strEndTime+"</td></tr>");
					
				});
				
				$(".productPageBar ul.pagination").initPageBar({
					pageNo:pageNo,
					pageSize:pageSize,
					prevPageNo:prevPageNo,
					nextPageNo:nextPageNo,
					totalPages:totalPages,
					totalResults:totalItems
				}, function(jumpTo, pageSize){
					ajaxSearchList(searchType,jumpTo,pageSize);
				});
			} else {
				error(json.msg || "查询失败");
			}
		});
	}
}



</script>
<!-- #set(PaginatedList<Product> products) -->
<!-- #set(PaginatedList<Order> orders) -->
<!--
		#set(
		pager = products.pager,
		totalResults = pager.getCountOfTotalResults(),
		totalPages = pager.getCountOfTotalPages() * 1,
		pageSize = pager.pageSize,
		pageNo = pager.pageNo,
		prePageNo = pager.getPrevPageNo(),
		nextPageNo = pager.getNextPageNo()
		)
-->
<!--
		#set(
		pager1 = orders.pager,
		totalResults1 = pager1.getCountOfTotalResults(),
		totalPages1 = pager1.getCountOfTotalPages() * 1,
		pageSize1 = pager1.pageSize,
		pageNo1 = pager1.pageNo,
		prePageNo = pager.getPrevPageNo(),
		nextPageNo = pager.getNextPageNo()
		)
-->
<div class="panel-heading panel-result-title">
	<h3 class="panel-title">查询结果</h3>
	<div id="product_operator"
		class="btn-group btn-gp-default">
		<button id="btn_add" type="button" onclick="addProduct()"
			class="btn btn-default panel-radius-left">添加</button>
		<button id="btn_edit" type="button"
			class="btn btn-default panel-radius-none">修改</button>
		<button id="btn_delete" type="button"
			class="btn btn-default panel-radius-none">删除</button>
		<button id="btn_export" type="button"
			class="btn btn-default panel-radius-right" onclick="exportList();" >导出</button>
	</div>
</div>
<div id="productTab" class="container-fluid">
	<div class="row-fluid">
		<div class="span8">
			<ul class="nav nav-tabs pro_ul">
				<li class="active"><a data="orderList" href="#orderList">购买记录</a></li>
				<li><a data="consumeList" href="#consumeList">出席记录</a></li>
				<li><a data="productList" href="#productList">产品列表</a></li>
			</ul>
		</div>
	</div>
</div>
<div class="tab-content">
<div id="consumeList" class="tab-pane">
	<table class="table table-bordered table-hover table-condensed tb-list-default tb" style="text-align: center;">
		<tr>
			<td class="tb-chose">会员编号</td>
			<td class="tb-chose">会员姓名</td>
			<td class="tb-time">班级</td>
			<td class="tb-time">课程时间</td>
			<td class="tb-chose">老师</td>
			<td class="tb-chose">助教</td>
			<td>场地</td>
			<td>产品</td>
			<td class="tb-time">出席时间</td>
		</tr>
	</table>
	<div class="consumePageBar">$!{include("page_bar.html")}</div>
</div>
<div id="productList"  class="tab-pane">
	<table
		class="table table-bordered table-hover table-condensed tb-list-default tb"
		style="text-align: center;">
		<tr>
			<td class="td_ck tb-chose"><input type="checkbox" id="chkallIds"></td>
			<td class="tb-chose">编号</td>
			<td style="width:160px;">名称</td>
			<td class="tb-type">类型</td>
			<td class="tb-type">价格(元)</td>
			<td class="tb-type">次数</td>
			<td class="tb-type">每周限制</td>
			<td class="tb-long-time" style="width:160px;">起始日期</td>
			<td class="tb-long-time" style="width:160px;">结束日期</td>
		</tr>
	</table>
	<div class="productPageBar">$!{include("page_bar.html")}</div>
</div>
<div id="orderList" class="tab-pane active">
	<table class="table table-bordered table-hover table-condensed tb"
		style="text-align: center;">
		<tr>
			<td class="tb-chose">下单编号</td>
			<td class="tb-teacher">会员</td>
			<td>产品名称</td>
			<td class="tb-teacher">销售</td>
			<td class="tb-teacher">金额(元)</td>
			<td class="tb-teacher">产品类型</td>
			<td class="tb-teacher">负责人</td>
			<td class="tb-long-time">购买时间</td>
		</tr>
	</table>
	<div class="orderPageBar">$!{include("page_bar.html")}</div>
</div>
</div>
<script type="text/javascript">
	$(function() {
		var ids = [];
		//修改
		$("#btn_edit").click(function() {
			$('input[name="chk_name"]:checked').each(function() {
				ids.push($(this).val());
			});
			 if (ids.length == 1) {
				window.location.href= "yueyogaAdmin/toEditProduct.jhtml?productId=" + ids;
			 } else {
				alert("请选择一行进行编辑!");
			}
			ids = [];
		});
		//删除
		$("#btn_delete").click(function() {
			$('input[name="chk_name"]:checked').each(function() {
				ids.push($(this).val());
			});
			
			if(ids.length > 0){
				confirm("确定要删除吗?", function() {
					this.close();
					var url = "yueyogaAdmin/deleteProductsByIds.jhtml?ids="+ids;
					var d = wait("正在删除，请稍候...");
					$.post(url,function(msg) {
						d.close();
						if (msg == "true") {
							ajaxSearchList("productList",1,10);
						} else {
							d.close();
							error("删除失败!");
						}
					});
				});
			}else{
				alert("请选择要删除的记录!");
			}
		});
		
		//--全选`反选
		$("#chkallIds").click(function() {
			$("input[name='chk_name']").prop("checked",$(this).prop("checked"));
		});
		
	});
	//导出产品的三个列表
	function exportList(){
		var action="";
		var oldAction="";
		if(shibie==1){
		    action = "yueyogaAdmin/exportProductList.jhtml";
		    oldAction = $("form[name='productListForm']").prop("action");
			$("form[name='productListForm']").prop("action",action);
			$("form[name='productListForm']").submit();
			$("form[name='productListForm']").prop("action",oldAction);
		}
		if(shibie==2){
			action = "yueyogaAdmin/exportOrderList.jhtml";
			oldAction = $("form[name='orderListForm']").prop("action");
			$("form[name='orderListForm']").prop("action",action);
			$("form[name='orderListForm']").submit();
			$("form[name='orderListForm']").prop("action",oldAction);
		}
		if(shibie==3){
			action = "yueyogaAdmin/exportConsumeList.jhtml";
			oldAction = $("form[name='consumeListForm']").prop("action");
			$("form[name='consumeListForm']").prop("action",action);
			$("form[name='consumeListForm']").submit();
			$("form[name='consumeListForm']").prop("action",oldAction);
		}
	}
	
	function changePage(searchType,value) {
		value = value * 1;
		var totalPages = 1;
		if(searchType=='2'){
			$("#txtPageNumber1").val(value);
			totalPages = '${totalPages1}';
		}else{
			$("#txtPageNumber").val(value);
			totalPages = '${totalPages}';
		}
		if (value > totalPages) {
			value = totalPages;
		} else if (value < 1) {
			value = 1;
		}
		goToPage(searchType,value);
	}
	function goToPage(searchType,pageNo){
		if(searchType==1){
			$("#hidPageNo").val(pageNo);
			document.productForm.submit();
		}else{
			$("#hidOrderPageNo").val(pageNo);
			document.productOrderForm.submit();
		}
	}
	function addProduct(){
		window.location.href= "yueyogaAdmin/toAddProduct.jhtml";
	}
</script>